home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 90 / CD Actual 90.iso / Software3D / K-3D / k3d-0.4.2.1 / shaders / k3d_terran.sl < prev    next >
Encoding:
Text File  |  2004-07-23  |  5.2 KB  |  177 lines

  1. /*
  2. mottle_limit, mottle_scale, moddle_dim, mottle_mag - control the
  3.            mottling that adds detail to lower latitude regions.
  4. */
  5.  
  6.  
  7. #ifdef BMRT
  8. #define snoise(x) (2*(noise(x)-0.5))
  9. #else
  10. /* This is because PRMAN's noise has less range than BMRT's */
  11. #define snoise(x) (2.5*(noise(x)-0.5))
  12. #endif
  13.  
  14. #define DNoise(x) ((2*(point noise(x))) - point(1,1,1))
  15. #define VLNoise(Pt,scale) (snoise(DNoise(Pt)+(scale*Pt)))
  16. #define N_OFFSET 0.7
  17. #define VERY_SMALL 0.0001
  18.  
  19.  
  20. surface k3d_terran(float Ka = .5, Kd = .7;
  21.            float spectral_exp = 0.5;
  22.            float lacunarity = 2, octaves = 7;
  23.            float multifractal = 0; float dist_scale = .2;
  24.            float offset = 0;
  25.            float sea_level = 0;
  26.            float mtn_scale = 1;
  27.            float lat_scale = 0.95;
  28.            float nonlinear = 0;
  29.            float purt_scale = .9; float map_exp = 0;
  30.            float ice_caps = 0.9; float depth_scale = 1;
  31.            float depth_max = .5; float mottle_limit = 0.75;
  32.            float mottle_scale = 20; float mottle_dim = .25;
  33.            float mottle_mag = .02;)
  34. {
  35.   point PP;
  36.   point PtN;
  37.   float chaos, latitude, purt;
  38.   color Ct;
  39.   point Ptexture, tp;
  40.   float l, o, a, i, weight;    /* Loop variables for fBm calc */
  41.   float bumpy;
  42.  
  43.   /* Do all shading in shader space */
  44.   Ptexture = transform("shader", P);
  45.   PtN = normalize(Ptexture);    /* Version of Ptexture with radius 1 */
  46.  
  47.     /**********************************************************************
  48.    * First, figure out where we are in relation to the oceans/mountains.
  49.    * Note: this section of code must be identical to "terranbump" if you
  50.    *       expect these two shaders to work well together.
  51.    **********************************************************************/
  52.  
  53.   if(multifractal == 0)
  54.     {                /* use a "standard" fBm bump function */
  55.       o = 1;
  56.       l = 1;
  57.       bumpy = 0;
  58.       for(i = 0; i < octaves; i += 1)
  59.     {
  60.       bumpy += o * snoise(l * Ptexture);
  61.       l *= lacunarity;
  62.       o *= spectral_exp;
  63.     }
  64.     }
  65.   else
  66.     {                /* use a "multifractal" fBm bump function */
  67.       /* get "distortion" vector, as used with clouds */
  68.       Ptexture += dist_scale * DNoise(Ptexture);
  69.       /* compute bump vector using MfBm with displaced point */
  70.       o = spectral_exp;
  71.       tp = Ptexture;
  72.       weight = abs(VLNoise(tp, 1.5));
  73.       bumpy = weight * snoise(tp);
  74.       for(i = 1; i < octaves && weight >= VERY_SMALL; i += 1)
  75.     {
  76.       tp *= lacunarity;
  77.       /* get subsequent values, weighted by previous value */
  78.       weight *= o * (N_OFFSET + snoise(tp));
  79.       weight = clamp(abs(weight), 0, 1);
  80.       bumpy += snoise(tp) * min(weight, spectral_exp);
  81.       o *= spectral_exp;
  82.     }
  83.     }
  84.  
  85.   /* get the "height" of the bump, displacing by offset */
  86.   chaos = bumpy + offset;
  87.   /* set bump for land masses (i.e., areas above "sea level") */
  88.   if(chaos > sea_level)
  89.     {
  90.       chaos *= mtn_scale;
  91. /*      sea_level *= mtn_scale; */
  92.     }
  93.  
  94.  
  95.     /************************************************************************
  96.    * Step 2: Assign a climite type, roughly by latitude.
  97.    ************************************************************************/
  98.  
  99.   /* make climate symmetric about equator -- use the "v" parameter */
  100.   latitude = abs(ycomp(PtN));
  101.  
  102.   /* fractally purturb color map offset using "chaos" */
  103.   /*  "nonlinear" scales purturbation-by-z */
  104.   /*  "purt_scale" scales overall purturbation */
  105.   latitude += chaos * (nonlinear * (1 - latitude) + purt_scale);
  106.   if(map_exp > 0)
  107.     latitude = lat_scale * pow(latitude, map_exp);
  108.   else
  109.     latitude *= lat_scale;
  110.  
  111.  
  112.   if(chaos > sea_level)
  113.     {
  114.       /* Choose color of land based on the following spline.
  115.        * Ken originally had a huge table.  I was too lazy to type it in,
  116.        * so I used a scanned photo of the real Earth to select some
  117.        * suitable colors.  -- lg
  118.        */
  119. /*
  120.       Ct = spline (latitude,
  121.            color (.529, .412, .2745),
  122.            color (.529, .412, .2745),
  123.            color (.529, .412, .2745),
  124.            color (.255, .341,  0),
  125.            color (.256, .341, .141),
  126.            color (.235, .392, .235),
  127.            color (.490, .494, .1176),
  128.            color (.655, .529, .392),
  129.            color (.769, .616, .314),
  130.            color (.976, .820, .471),
  131.            color (1,1,1),
  132.            color (1,1,1));
  133. */
  134.       Ct =
  135.     spline(latitude, color(.5, .39, .2), color(.5, .39, .2),
  136.            color(.5, .39, .2), color(.2, .3, 0), color(.085, .2, .04),
  137.            color(.065, .22, .04), color(.5, .42, .28), color(.6, .5, .23),
  138. /*           color (.976, .820, .471), */
  139.            color(1, 1, 1), color(1, 1, 1));
  140.  
  141.       /* mottle the color some */
  142.       if(latitude < mottle_limit)
  143.     {
  144.       PP = mottle_scale * Ptexture;
  145.       l = 1;
  146.       o = 1;
  147.       for(i = 0; i < 6; i += 1)
  148.         {
  149.           purt += o * snoise(l * PP);
  150.           l *= 2;
  151.           o *= mottle_dim;
  152.         }
  153.       Ct += (mottle_mag * purt) * (color(0.5, 0.175, 0.5));
  154.     }
  155.     }
  156.   else
  157.     {
  158.       /* Oceans */
  159.       Ct = color(.1, .2, .5);
  160.       if(ice_caps > 0 && latitude > ice_caps)
  161.     Ct = color(1, 1, 1);    /* Ice color */
  162.       else
  163.     {
  164.       /* Adjust color of water to darken deeper seas */
  165.       chaos -= sea_level;
  166.       chaos *= depth_scale;
  167.       chaos = max(chaos, -depth_max);
  168.       Ct *= (1 + chaos);
  169.     }
  170.     }
  171.  
  172.   /* Shade using matte model */
  173.   Oi = Os;
  174.   Ci =
  175.     Os * Ct * (Ka * ambient() + Kd * diffuse(faceforward(normalize(N), I)));
  176. }
  177.